let data = {
  pagenum: 1,
  pagesize: 3

}

function userlist() {
  $.ajax({
    url: '/admin/users',
    data: data,
    success: function (res) {
      let htmlStr = template('user-list', res)
      $('tbody').html(htmlStr)
      showPage(res.total)
    }
  })
}
userlist()

let laypage = layui.laypage

function showPage(t) {
  laypage.render({
    elem: 'page',
    count: t,
    limit: data.pagesize,
    curr: data.pagenum,
    groups: 5,
    limits: [2, 3, 5, 10],
    layout: ['limit', 'page', 'prev', 'next', 'count', 'skip'],
    jump: function (obj, first) {
      if (first === undefined) {
        data.pagenum = obj.curr
        data.pagesize = obj.limit
        userlist()
      }
    }
  });

}

// 删除账号
function del() {
  $('tbody').on('click', 'button:contains("删除")', function (ev) {
    layer.confirm('确认要删除用户吗?', {
      title: '信息'
    }, function (index) {
      layer.close(index);
      let id = $(ev.target).data('id');

      function delUser() {
        $.ajax({
          url: '/admin/users/' + id,
          type: 'DELETE',
          success: function (res) {
            layer.msg(res.message);
            userlist();
          }
        })
      }
      delUser();
    });
  })
}
del();

// 重置密码
let index;
$('tbody').on('click', 'button:contains("重置")', function () {
  index = layer.open({
    type: 1,
    area: ["500px", "250px"],
    title: '重置密码',
    content: $('#tpl-reset').html(),
  });

  let id = $(this).data('id');
  $('body').on('submit', '#re', function (ev) {
    ev.preventDefault();
    let data = $(this).serialize();
    $.ajax({
      url: '/admin/users/' + id,
      type: 'PUT',
      data: data,
      success: function (res) {
        // console.log(res)
        layer.msg(res.message);
        layer.close(index);
      }
    })
  })
});